package com.rc.saas.tenant.service.channel;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.channel.RcChannelApplyMapper;
import com.rc.saas.tenant.model.channel.*;
import com.rc.saas.tenant.model.enterprise.EnterpriseMember;
import com.rc.saas.tenant.model.enterprise.EnterpriseSubmemberApply;
import com.rc.saas.tenant.model.enterprise.EnterpriseSubmemberApplyExample;
import com.rc.saas.tenant.model.enterprise.EnterpriseSubmemberExample;
import com.rc.saas.tenant.model.member.RcMemberFlag;
import com.rc.saas.tenant.service.enterprise.EnterpriseMemberService;
import com.rc.saas.tenant.service.enterprise.EnterpriseSubmemberApplyService;
import com.rc.saas.tenant.service.member.RcMemberFlagService;
import com.rc.saas.tenant.vo.channel.RcChannelApplyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RcChannelApplyServiceImpl implements RcChannelApplyService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private RcChannelApplyMapper channelApplyMapper;

    @Autowired
    private RcMemberFlagService memberFlagService;

    @Autowired
    private RcChannelGainRecordService channelGainRecordService;

    @Autowired
    private RcChannelGainSettingService channelGainSettingService;

    @Autowired
    private EnterpriseSubmemberApplyService enterpriseSubmemberApplyService;

    @Override
    public RcChannelApplyMapper getMapper() {
        return channelApplyMapper;
    }

    @Override
    public PageInfo listPageChannelApply(Long entpriseId, String channelName, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<RcChannelApplyVo> list = channelApplyMapper.listChannelApply(entpriseId, channelName);
        list.stream()
            .filter(apply -> "1".equals(apply.getApplyStatus())  && apply.getMemberId() != null)
            .forEach(apply -> {
//                RcChannelGainRecordExample example = new RcChannelGainRecordExample();
//                example.createCriteria().andChannelIdEqualTo(apply.getChannelApplyId());
//                RcChannelGainRecord gainRecord = channelGainRecordService.selectByExample(example).stream().findFirst().orElseGet(() -> null);
//                if (gainRecord != null) {
//                    apply.setChannelLevel(gainRecord.getChannelLevel());
//                }

                RcMemberFlag memberFlag = memberFlagService.selectByPrimaryKey(apply.getMemberId());
                if (memberFlag != null) {
                    apply.setChannelLevel(memberFlag.getChannelLevel());
                }
        });

        return new PageInfo(list);
    }

    @Override
    public void updateChannelApplyForAudit(Long channelApplyId, String applyStatus, String remark) {
        RcChannelApply channelApply = channelApplyMapper.selectByPrimaryKey(channelApplyId);

        if ("1".equals(applyStatus) && channelApply.getMemberId() != null) {
            memberFlagService.addChannel(channelApply.getMemberId(), channelApply.getEnterpriseId());
            channelApply.setStatus("1");

            RcChannelGainSettingExample gainSettingExample = new RcChannelGainSettingExample();
            gainSettingExample.createCriteria().andChannelLevelEqualTo("S0");
            List<RcChannelGainSetting> gainSettingList = channelGainSettingService.selectByExample(gainSettingExample);
            for (RcChannelGainSetting gainSetting:gainSettingList) {
                RcChannelGainRecord gainRecord = new RcChannelGainRecord();
                gainRecord.setChannelLevel(gainSetting.getChannelLevel());
                gainRecord.setMemberId(channelApply.getMemberId());
                gainRecord.setChannelId(channelApplyId);
                gainRecord.setGainName(gainSetting.getGainName());
                gainRecord.setGainType(gainSetting.getGainType());
                gainRecord.setGainLogo(gainSetting.getGainLogo());
                gainRecord.setRebateRatio(gainSetting.getRebateRatio());
                gainRecord.setGainExplain(gainSetting.getGainExplain());
                gainRecord.setSort(gainSetting.getSort());
                channelGainRecordService.insertSelective(gainRecord);
            }



        }
        channelApply.setApplyStatus(applyStatus);
        channelApply.setRemark(remark);
        channelApplyMapper.updateByPrimaryKeySelective(channelApply);

    }

    @Override
    public void deleteChannelApply(Long id) {
        RcChannelApply channelApply = channelApplyMapper.selectByPrimaryKey(id);
        if ("1".equals(channelApply.getApplyStatus())
            && "1".equals(channelApply.getStatus())
            && channelApply.getMemberCard() != null) {
            memberFlagService.delChannel(channelApply.getMemberId());
        }
        channelApplyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public RcChannelApply selectChannelApplyByChannel(String channel) {
        return channelApplyMapper.selectChannelApplyByChannel(channel);
    }

    @Override
    public void insertReturnId(RcChannelApply rcChannelApply) {
        channelApplyMapper.insertReturnId(rcChannelApply);
    }

    @Override
    public void updateEnterpriseId(RcChannelApply channelApply) {
        log.info("updateEnterpriseId:enterpriseId:",channelApply.getEnterpriseId());
        channelApplyMapper.updateByPrimaryKeySelective(channelApply);
        EnterpriseSubmemberApply submember = new EnterpriseSubmemberApply();
//        submember.setMemberId(channelApply.getMemberId());
        submember.setEnterpriseId(channelApply.getEnterpriseId());

        EnterpriseSubmemberApplyExample enterpriseSubmemberApplyExample = new EnterpriseSubmemberApplyExample();
        enterpriseSubmemberApplyExample.createCriteria().andMemberIdEqualTo(channelApply.getMemberId());
        enterpriseSubmemberApplyService.updateByExampleSelective(submember,enterpriseSubmemberApplyExample);
    }
}
